package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.Clinic;
import com.csr.csrcheck.pojo.Product;
import com.csr.csrcheck.service.ClinicService;
import com.csr.csrcheck.service.ProductService;
import com.csr.csrcheck.service.ex.ClinicException;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping({"clinic"})
@RestController
public class ClinicController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.ClinicController.class);

    @Resource
    private ClinicService clinicService;

    @Resource
    private ProductService productService;

    @GetMapping({"cliniclist"})
    public JsonResult<List<Clinic>> Flight_checklist(HttpServletResponse response) throws IOException, ServletException {
        List<Clinic> list = this.clinicService.getCliniclist();
        if (list == null)
            throw new ClinicException("");
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @GetMapping({"clinic"})
    public JsonResult<Object> clinic(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String product_name) {
        PageResult pageResult = this.clinicService.getListpage(pageNum, pageSize, product_name);
        log.info("clinic---------------------------->pageNum:" + pageNum);
        log.info("clinic---------------------------->pageSize:" + pageSize);
        log.info("clinic---------------------------->product_name:" + product_name);
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping(path = {"addclinic"}, method = {RequestMethod.POST})
    public JsonResult<Object> addclinic(Clinic clinic) {
        this.clinicService.addClinic(clinic);
        log.info("add================================>approvals"+ clinic);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"listproduct"}, method = {RequestMethod.POST})
    public JsonResult<List<Product>> listproduct() {
        List list = this.productService.list();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"updateclinic"}, method = {RequestMethod.POST})
    public JsonResult<Void> updateclinic(Clinic clinic) {
        this.clinicService.updateClinic(clinic);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findClinicByid/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Clinic> findCompanyByid(@PathVariable("id") int id) {
        Clinic list = null;
        try {
            list = this.clinicService.findclinicByid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"deleteclinic/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deleteclinic(@PathVariable("id") int id) {
        this.clinicService.deleteclinicByid(id);
        log.info(""+ id);
        return new JsonResult(Integer.valueOf(0), "json");
    }
}
